package com.e3.basdoc.warehouse.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.warehouse.dao.WarehouseDAO;
import com.e3.basdoc.warehouse.entity.WarehouseEntity;
import com.e3.basdoc.warehouse.model.WarehouseModel;
import com.e3.basdoc.warehouse.model.WarehouseQC;
import com.e3.basdoc.warehouse.service.WarehouseCacheManager;
import com.e3.basdoc.warehouse.service.WarehouseQueryService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperQueryService;

public class WarehouseQueryServiceImpl extends SuperQueryService implements
		WarehouseQueryService {
	
	private WarehouseDAO warehouseDAO;
	
	private WarehouseDAO getWarehouseDAO(){
		if(warehouseDAO == null){
			warehouseDAO = DAOHelper.createDAOInstance(WarehouseDAO.class);
		}
		return warehouseDAO;
	}

	public List<WarehouseModel> getWarehouseByCondition(WarehouseQC qc) throws ServiceException {
		
		List<Long> ids = getWarehouseDAO().getWarehouseIdsByCondition(qc);
		if(ids == null||ids.isEmpty()){
			return null;
		}
		
		List<WarehouseModel> models = new ArrayList<WarehouseModel>(ids.size());
		for (Long id : ids) {
			WarehouseModel model = getWarehouseById(id);
			models.add(model);
		}
		return models;
	}
	/**
	 * the method is used to load data into cache.
	 */
	public List<WarehouseModel> getAllWarehouses() throws ServiceException {
		List<WarehouseEntity> entities = getWarehouseDAO().getAllWarehouses();
		if(entities==null||entities.isEmpty()){
			return null;
		}
		List<WarehouseModel> models = new ArrayList<WarehouseModel>(entities.size());
		for (WarehouseEntity warehouseEntity : entities) {
			models.add(warehouseEntityToModel(warehouseEntity));
		}
		return models;
	}

	public WarehouseModel getWarehouseById(long id) throws ServiceException {
		WarehouseModel model = WarehouseCacheManager.INSTANCE.get(id);
		if(model==null){
			WarehouseEntity entity = getWarehouseDAO().getById(WarehouseEntity.class, id);
			model = warehouseEntityToModel(entity);
			if(model != null){
				WarehouseCacheManager.INSTANCE.put(model);
			}
		}
		
		return model;
	}

	public List<WarehouseModel> getWarehouseByIds(List<Long> ids)	throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return null;
		}
		
		List<WarehouseModel> models = new ArrayList<WarehouseModel>(ids.size());
		for (Long id : ids) {
			WarehouseModel model = getWarehouseById(id);
			models.add(model);
		}
		
		return models;
	}

	private WarehouseModel warehouseEntityToModel(WarehouseEntity entity){
		
		if(entity == null){
			return null;
		}
		
		WarehouseModel model = new WarehouseModel();
		model = ocEntityToModel(model, entity);
		
		model.setCode(entity.getCode());
		model.setName(entity.getName());
		model.setAdministrator(entity.getAdministrator());
		model.setContractWay(entity.getContractWay());
		model.setStatus(entity.getStatus());
		model.setType(entity.getType());
		
		return model;
	}
}
